package svn;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.File;
import java.util.Map;
import java.util.Set;

public class SVNLogClientExample {
    public static void main(String[] args) {
        // 初始化SVN库
        setupLibrary();

        // 本地SVN仓库路径
        File repositoryFile = new File("https://192.168.0.71/svn/S深圳市卡司通展览股份");

        // 创建SVNLogClient实例
        SVNClientManager clientManager = SVNClientManager.newInstance();
        SVNLogClient logClient = clientManager.getLogClient();

        try {
            // 获取本地SVN仓库的URL
            String url = repositoryFile.toURI().toURL().toString();

            // 通过URL创建SVNURL实例
            SVNURL svnURL = SVNURL.parseURIDecoded("https://192.168.0.71/svn/S深圳市卡司通展览股份");

            // 设置身份验证信息，如果需要的话
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("shxl2021", "ff8d7o0rmc2j7");
            clientManager.setAuthenticationManager(authManager);

            // 获取仓库的日志信息
            logClient.doLog(svnURL, null, SVNRevision.HEAD, SVNRevision.create(0), SVNRevision.HEAD, false, true, 0,
                    new ISVNLogEntryHandler() {
                        @Override
                        public void handleLogEntry(SVNLogEntry logEntry) {
                            System.out.println("Revision: " + logEntry.getRevision());
                            System.out.println("Author: " + logEntry.getAuthor());
                            System.out.println("Date: " + logEntry.getDate());
                            System.out.println("Log Message: " + logEntry.getMessage());

                            // 获取修改的文件信息
                            Map<String, SVNLogEntryPath> changedPaths = logEntry.getChangedPaths();
                            Set<Map.Entry<String, SVNLogEntryPath>> changedPathsSet = changedPaths.entrySet();
                            for (Map.Entry<String, SVNLogEntryPath> entry : changedPathsSet) {
                                SVNLogEntryPath entryPath = entry.getValue();
                                SVNNodeKind nodeKind = entryPath.getKind();
                                String path = entryPath.getPath();
                                SVNRevision copyRevision = SVNRevision.create(entryPath.getCopyRevision());
                                System.out.println("Changed Path: " + path);
                                System.out.println("Node Kind: " + nodeKind);
                                System.out.println("Copy Revision: " + copyRevision);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setupLibrary() {
        // 初始化SVN库
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
    }
}
